package com.mms.utils;

import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * Created by dan3652@sina.com on 2016-12-02 10:59.
 */
public class XmlConvertUtils {
    private static final String MAP = "map";
    private static final String LIST = "list";

    public XmlConvertUtils() {
    }

    public static Element maptoXml(Map<String, Object> map) {
        Element element = DocumentHelper.createElement("map");
        Iterator var3 = map.keySet().iterator();

        while(var3.hasNext()) {
            String key = (String)var3.next();
            Object value = map.get(key);
            Element entry = element.addElement(key.toLowerCase());
            if(value instanceof Map) {
                entry.add(maptoXml((Map)value));
            } else if(value instanceof List) {
                entry.add(listtoXml((List)value));
            } else if(value == null) {
                entry.addText("");
            } else {
                entry.addText(value.toString());
            }
        }

        return element;
    }

    public static Element maptoXmlV2(Map<String, Object> map) {
        Element element = DocumentHelper.createElement("map");
        Iterator var3 = map.keySet().iterator();

        while(var3.hasNext()) {
            String key = (String)var3.next();
            Object value = map.get(key);
            Element entry = element.addElement(key);
            if(value instanceof Map) {
                entry.add(maptoXmlV2((Map)value));
            } else if(value instanceof List) {
                entry.add(listtoXmlV2((List)value));
            } else if(value == null) {
                entry.addText("");
            } else {
                entry.addText(value.toString());
            }
        }

        return element;
    }

    public static Element listtoXml(List list) {
        Element element = DocumentHelper.createElement("list");
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            Object o = var3.next();
            if(o instanceof Map) {
                element.add(maptoXml((Map)o));
            }
        }

        return element;
    }

    public static Element listtoXmlV2(List list) {
        Element element = DocumentHelper.createElement("list");
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            Object o = var3.next();
            if(o instanceof Map) {
                element.add(maptoXmlV2((Map)o));
            }
        }

        return element;
    }

    public static Map xmltoMap(Element element) {
        try {
            HashMap e = new HashMap();
            String node = element.getName();
            if("map".equals(node)) {
                List entrys = element.elements();
                Iterator var5 = entrys.iterator();

                while(var5.hasNext()) {
                    Element e1 = (Element)var5.next();
                    String key = e1.getName();
                    Node ele = null;
                    if((ele = e1.selectSingleNode("map")) != null) {
                        e.put(key, xmltoMap((Element)ele));
                    } else if((ele = e1.selectSingleNode("list")) != null) {
                        e.put(key, xmltoList((Element)ele));
                    } else {
                        String value = e1.getStringValue();
                        e.put(key, value);
                    }
                }
            }

            return e;
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static Map xmltoMapV2(Element element) {
        try {
            HashMap e = new HashMap();
            String node = element.getName();
            if("map".equals(node)) {
                List entrys = element.attributes();
                Iterator var5 = entrys.iterator();

                while(var5.hasNext()) {
                    Element e1 = (Element)var5.next();
                    String key = e1.getName();
                    Node ele = null;
                    if((ele = e1.selectSingleNode("map")) != null) {
                        e.put(key, xmltoMapV2((Element)ele));
                    } else if((ele = e1.selectSingleNode("list")) != null) {
                        e.put(key, xmltoListV2((Element)ele));
                    } else {
                        String value = e1.getStringValue();
                        e.put(key, value);
                    }
                }
            }

            return e;
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static List xmltoList(Element element) {
        try {
            ArrayList e = new ArrayList();
            String node = element.getName();
            if("list".equals(node)) {
                List maps = element.selectNodes("map");
                Iterator var5 = maps.iterator();

                while(var5.hasNext()) {
                    Element ele = (Element)var5.next();
                    e.add(xmltoMap(ele));
                }
            }

            return e;
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static List xmltoListV2(Element element) {
        try {
            ArrayList e = new ArrayList();
            String node = element.getName();
            if("list".equals(node)) {
                List maps = element.selectNodes("map");
                Iterator var5 = maps.iterator();

                while(var5.hasNext()) {
                    Element ele = (Element)var5.next();
                    e.add(xmltoMapV2(ele));
                }
            }

            return e;
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static Element string2Doc(String xmlString) {
        StringReader source = new StringReader(xmlString);
        SAXReader reader = new SAXReader();
        Document doc = null;

        try {
            doc = reader.read(source);
        } catch (DocumentException var5) {
            var5.printStackTrace();
        }

        return doc.getRootElement();
    }

    public static Element reader2Doc(Reader source) {
        SAXReader reader = new SAXReader();
        Document doc = null;

        try {
            doc = reader.read(source);
        } catch (DocumentException var4) {
            var4.printStackTrace();
        }

        return doc.getRootElement();
    }

    public static Element transform(DOMSource source) {
        try {
            StringWriter e = new StringWriter();
            StreamResult result = new StreamResult(e);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(source, result);
            return string2Doc(e.getBuffer().toString());
        } catch (TransformerConfigurationException var5) {
            var5.printStackTrace();
        } catch (TransformerFactoryConfigurationError var6) {
            var6.printStackTrace();
        } catch (TransformerException var7) {
            var7.printStackTrace();
        }

        return null;
    }

    public static void main(String[] args) {
        HashMap map1 = new HashMap();
        map1.put("A3", "<!a3");
        map1.put("A4", "a4");
        ArrayList list = new ArrayList();
        list.add(map1);
        HashMap map2 = new HashMap();
        map2.put("A5", "a5");
        map2.put("A6", "a6");
        list.add(map2);
        HashMap map = new HashMap();
        map.put("A1", "a1");
        map.put("A2", list);
        String xml = maptoXmlV2(map).asXML();
        System.out.println(xml);
    }
}
